import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import goodsRouter from './modules/ygr_goods_router.js'
import youhuiRouter from './modules/hjw_houhui_router.js'
import orderRouter from './modules/lyp_order_router.js'
import gonghuoRouter from './modules/laj_gonghuo_router.js'
import indexRouter from './modules/cf_index_router.js'
import store from '../store/index';
import { Message } from 'element-ui'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      required: false
    }
  },
  {
    path: '/index',
    component: () => import(/* webpackChunkName: "about" */ '../views/Index.vue'),
    children: [
      
        // 商品列表
       ...goodsRouter,
       ...orderRouter,
       ...gonghuoRouter,
      ...youhuiRouter,
      ...indexRouter,
    ],
    meta: {
      required: true,
      role: ['admin','user']
    }
  },
  { path: '/',
    redirect: '/login',
    // component: Login
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next) => {
  // 判断进入的页面是什么页面
  // 如果是不需要登录就可以进入的页面,放行
  if (!to.meta.required) return next()
  // 这里是需要登录的页面 判断有没有登录
  // console.log(store.state.indexStore.username)
    if (store.state.indexStore.username) {
  //  登录之后,看需不需要权限验证?
  //   console.log('已经登录,即将要跳转页面,可不可以访问这个页面', to.meta.role.includes(store.state.indexStore.level))
    if (to.meta.role.includes(store.state.indexStore.level)) return next()
    // 这里是没有权限
    Message.error('您暂时没有权限访问该页面')
    return
  //  TODO: 完善权限
  
  }
  // 没有登录就去登录页面登录
  router.push('/login')
})

export default router
